<template>
  <div>
    <div class="list-table">
      <div class="area-header">
        <span class="area-header-title">{{ $t('合同管理') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('申请标题')" prop="actTitle">
          <el-input
            v-model="queryParams.actTitle"
            :placeholder="$t('申请标题')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item :label="$t('交易模式')" prop="transactionMode">
          <el-select v-model="queryParams.transactionMode" :placeholder="$t('交易模式')" clearable>
            <el-option
              v-for="dict in transactionModeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('合同编号')" prop="contractNo">
          <el-input
            v-model="queryParams.contractNo"
            :placeholder="$t('合同编号')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['financing:contract:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t('新增') }}</el-button>
        </el-col>
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          v-hasPermi="['financing:contract:export']"-->
        <!--          type="warning"-->
        <!--          icon="el-icon-download"-->
        <!--          size="mini"-->
        <!--          @click="exportHandle"-->
        <!--        >{{ $t('导出') }}</el-button>-->
        <!--      </el-col>-->
      </el-row>

      <el-table v-loading="loading" :data="dealLeaseContractList" border tooltip-effect="darkTable">
        <el-table-column type="index" align="center" show-overflow-tooltip width="55">
          <template slot-scope="scope">
            <el-radio v-model="radioView" :label="(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1" @click.native.prevent="choiceRadio(scope.$index, scope.row)">
              <span style="display: none;" />
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
          <template slot-scope="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('申请标题')" align="center" prop="actTitle" width="180">
          <template slot-scope="scope">
            <el-link class="link-a" type="primary" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</el-link>
          </template>
        </el-table-column>
        <el-table-column :label="$t('合同总额')+$t('金额单位')" align="center" prop="totalContractAmount" width="180">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.totalContractAmount,'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('合同编号')" align="center" prop="contractNo" width="180" />
        <el-table-column :label="$t('租金总额')+$t('金额单位')" align="center" prop="totalRent" width="180">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.totalRent,'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('租聘利率（%）')" align="center" prop="leaseRate" width="180" />
        <el-table-column :label="$t('税率（%）')" align="center" prop="taxRate" width="180" />
        <el-table-column :label="$t('XIRR（%）')" align="center" prop="xirr" width="180" />
        <el-table-column :label="$t('手续费/咨询费')+$t('金额单位')" align="center" prop="serviceCharge" width="180">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.serviceCharge,'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('保证金')+$t('金额单位')" align="center" prop="earnestMoney" width="180">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.earnestMoney,'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('起租日')" align="center" prop="commencementDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.commencementDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('到期日')" align="center" prop="expiringDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expiringDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('期限（月）')" align="center" prop="term" width="180" />
        <el-table-column :label="$t('担保人')" align="center" prop="guarantee" width="180" />
        <el-table-column :label="$t('审批状态')" align="center" fixed="right" width="120">
          <template slot-scope="scope">
            <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
          <template slot-scope="scope">
            <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
            <ListBtn :bus-data="scope.row" has-permi="financing:contract" />
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改合同管理对话框 -->
      <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
        <dealLeaseContractForm ref="dealLeaseContractForm" />
      </el-dialog>
    </div>
    <!-- 关键条款 -->
    <div style="height: 20px" />
    <dealTermsList ref="dealTermsList" has-permi="financing:contract" />
  </div>
</template>

<script>
import { listDealLeaseContract, delDealLeaseContract, exportDealLeaseContract } from '@/api/deal/financing/contract/dealLeaseContract'
import dealLeaseContractForm from './dealLeaseContractForm.vue'
import dealTermsList from '@/views/deal/terms/dealTermsList.vue'
export default {
    components: {
      dealTermsList,
      dealLeaseContractForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 合同管理表格数据
          dealLeaseContractList: [],
          // 弹出层标题
          title: '合同管理',
          radioView: '',
          // 是否显示弹出层
          formOpen: false,
        // 交易模式字典
        transactionModeOptions: [],

          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              actTitle: undefined,
              transactionMode: undefined,
              contractNo: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('financing_transaction_mode').then(response => {
          this.transactionModeOptions = response.data
      })
  },
  methods: {
    /** 查询合同管理列表 */
    getList() {
        this.loading = true
        listDealLeaseContract(this.queryParams).then(response => {
            this.dealLeaseContractList = response.rows
            this.total = response.total
            this.loading = false
            this.$refs.dealTermsList.getList()
        }).catch(() => {
            this.loading = false
        })
    },
    // 交易模式字典翻译
    transactionModeFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.transactionModeOptions, row.transactionMode)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: undefined,
            actTitle: undefined,
            actId: undefined,
            actStatus: '0',
            lesseeName: undefined,
            dealName: undefined,
            transactionMode: undefined,
            customerContact: undefined,
            totalContractAmount: undefined,
            contractNo: undefined,
            totalRent: undefined,
            leaseRate: undefined,
            taxRate: undefined,
            xirr: undefined,
            serviceCharge: undefined,
            earnestMoney: undefined,
            commencementDate: undefined,
            expiringDate: undefined,
            term: undefined,
            guarantee: undefined,
            guaranteeMethod: undefined,
            otherGuaranteeMethod: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealLeaseContractForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealLeaseContractForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delDealLeaseContract(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
          this.loading = false
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return exportDealLeaseContract(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(() => {
      })
    },
    choiceRadio(index, row) {
      if (this.radioView === (index + 1)) {
        this.radioView = ''
        this.$nextTick(() => {
          this.$refs.dealTermsList.queryParams.associatedContract = ''
          this.$refs.dealTermsList.queryParams.type = undefined
          this.$refs.dealTermsList.getList()
        })
      } else {
        this.radioView = index + 1
        this.$nextTick(() => {
          this.$refs.dealTermsList.queryParams.associatedContract = row.id
          this.$refs.dealTermsList.queryParams.type = undefined
          this.$refs.dealTermsList.getList()
        })
      }
    }
  }
}
</script>
